#include <stdio.h>
#include <stdlib.h>

int main()
{
////    int nvmax,ntmax,nbmax;
////////    nvmax   - maximum number of mesh nodes
////////    ntmax   - maximum number of mesh triangles
////////    nbmax   - maximum number of boundary edges
////
////    nvmax = 150000;
////    ntmax = 2 * nvmax;
////    nbmax = 10000;
////
////    double * vbr =  (double *)malloc( sizeof(double) * 2 * nbmax);
////    int Nbr, Nvr;
////    int * brd = (int *) malloc(sizeof(int) * 2 * nbmax);
////    int nv, nt, nb;
////
////    double * vrt = (double *) malloc( sizeof(double) *2 * nvmax);
////    int *material = (int *) malloc( sizeof(int) *ntmax);
////    int *tri = (int *) malloc( sizeof( int) *3 * ntmax);
////    int *bnd = (int *) malloc( sizeof( int) *4 * nbmax);
////
////    FILE * file = fopen("D:\\workSpace\\codeblock\\SWMM\\Ani2dAft\\front1", "r");
////
////    fscanf(file, "%d%d",  &Nvr, &Nbr);
////    printf("numof %d, %d", Nvr, Nbr);
////
////    int i = 0;
////    int j = 0;
////
////    for(i = 0 ; i < Nvr; i ++)
////    {
////        double val = 1.0;
////        double val2 = 1.0;
////        fscanf(file, "%lf %lf", &val, &val2);
////        printf("vbr[%d]=%lf,%lf\n",i,val,val2);
////        vbr[i * 2 ] = val;
////        vbr[i * 2 + 1] = val2;
////    }
////
////    for( i = 0 ; i < Nbr; i ++)
////    {
////        fscanf(file, "%d %d", &brd[ i * 2 + 1], &brd[2 * i]);
////        printf("brd[%d]=%d,%d\n",i, brd[ i * 2 ], brd[2 * i + 1 ]);
////    }
////
////    fclose(file);
////
////    aft2dfront_( &Nbr, brd, &Nbr,vbr,
////                 &nv, vrt,
////                 &nt, tri, material,
////                 &nb, bnd );
////
////    free(vbr);
////    free(brd);
////    free(vrt);
////    free(tri);
////    free(bnd);
////    free(material);
////
////    printf("Number of triangles=%d,vertices=%d\n", nt, nv);
//
//
//
//
////////////
    int nvmax,ntmax,nbmax;
////    nvmax   - maximum number of mesh nodes
////    ntmax   - maximum number of mesh triangles
////    nbmax   - maximum number of boundary edges

    nvmax = 150000;
    ntmax = 2 * nvmax;
    nbmax = 10000;

    double * vbr =  (double *)malloc( sizeof(double) * 2 * nbmax);
    int Nbr;
    int * brd = (int *) malloc(sizeof(int) * 2 * nbmax);
    int nv, nt, nb;

    double * vrt = (double *) malloc( sizeof(double) *2 * nvmax);
    int *material = (int *) malloc( sizeof(int) *ntmax);
    int *tri = (int *) malloc( sizeof( int) *3 * ntmax);
    int *bnd = (int *) malloc( sizeof( int) *4 * nbmax);

    FILE * file = fopen("D:\\workSpace\\codeblock\\SWMM\\Ani2dAft\\front2", "r");

    fscanf(file, "%d",&Nbr);

    int i = 0;
    int j = 0;


    for(i = 0 ; i < Nbr; i ++)
    {
        double val = 1.0;
        double val2 =  1.0;
        fscanf(file, "%lf %lf", &val, &val2);
        vbr[2*i]= val;
        vbr[2*i + 1] = val2;
        //printf("vbr[%d]=%lf,%lf\n", i, vbr[2 * i], vbr[2 * i + 1]);

    }




    fclose(file);


    int dump;
    int nullNbr = 0;
    aft2dfront_( &nullNbr, &dump, &Nbr, vbr,
                 &nv, vrt,
                 &nt, tri, material,
                 &nb, bnd );


    FILE *result = fopen("D:\\workSpace\\codeblock\\SWMM\\Ani2dAft\\result2.txt","w+");
    int iter = 0;

    //vertics
    for( iter = 0 ; iter < nv; iter ++)
    {
         printf("tri[%d]=%10lf,%10lf\n",iter, vrt[2 * iter], vrt[2 * iter + 1]);
         fprintf(result, "%10lf %10lf\n", vrt[2 * iter], vrt[2 * iter + 1]);
    }

    //triangles
    for(iter = 0 ; iter < nt; iter ++)
    {
        printf("tri[%d]=%5d,%5d,%5d,%5d\n",iter, tri[3*iter],tri[3*iter + 1],tri[3*iter + 2], material[iter]);
        fprintf(result,"%5d %5d %5d \n",tri[3*iter] , tri[3*iter + 1],tri[3*iter + 2]);
    }

    printf("nb=%d", nb);
    //boundary
    for(iter = 0 ; iter < nb; iter ++)
    {
        printf("bnd[%d]=%5d,%5d,%5d,%5d\n",iter, bnd[4*iter],bnd[4*iter + 1],bnd[4*iter + 2], bnd[4 * iter + 3]);
        fprintf(result, "%5d %5d\n",bnd[4*iter],bnd[4*iter + 1]);
    }


    fclose(result);
    free(vbr);
    free(brd);
    free(vrt);
    free(tri);
    free(bnd);
    free(material);

    printf("Number of triangles=%d,vertices=%d\n", nt, nv);
    printf("Hello world!\n");
    return 0;
}
